package gameComponents;
import gamePieces.Bishop;
import gamePieces.King;
import gamePieces.Knight;
import gamePieces.Pawn;
import gamePieces.Piece;
import gamePieces.Queen;
import gamePieces.Rook;

import java.io.IOException;
import java.io.StringReader;

/**
 * The Board class creates instances of 8*8 chess boards.
 * The boards are initialized to hold empty cells ("|_|").
 * As chess pieces are added, their corresponding letter value is added into a cell
 * on the chess board based on the x and y coordinates given. 
 * Queen - "Q", King - "G", Rook - "R", Knight - "T", Bishop - "B", Pawn - "P"
 * @author Selman Selman
**/

public class Board {
	// Default size of the grid columns and rows for the chess board.
	public static int SIZE = 8; 
	// Defines a 2-dimensional array made up of string cells.
	private Piece grid[][]; 
	private int beg = 0; // A variable that is used in the class
	
	/**
	 * Constructor for class board creates a 2-Dimensional Piece array (to act as a 
	 * the chess board). The 8*8 grid is initialized to hold empty cells, as null
	 * pieces.
	 * @modified Vivian Andreeva
	 */
	public Board(){
		this.grid = new Piece[SIZE][SIZE]; // Creates the grid if width = length = SIZE
		
		// loop through the grid and fill the cells with false
		for(int i = beg; i < grid.length; i++){
			for(int j = beg; j< grid.length; j++){
				grid[i][j] = null; // fill the grid with empty cells
			}
		}
	}
	
	/**
	 * Default getter for Board Object.
	 * 
	 * @author Jordan Beland
	 * @return - grid, a 2d Array
	 */
	public Piece[][] getBoard() {
		return grid;
	}
	
	/**
	 * Places a chess piece on the board. 
	 * @param i of type int : The x-coordinate of the piece
	 * @param j of type int : The y-coordinate of the piece
	 * @param p of type String : Identifies which piece is being placed (i.e Queen: "Q")
	 * @modified Vivian Andreeva
	 */
	public void placePiece(int i, int j, String p, Player player){
		if (p.equals("N") || p.equals("n")) {
			grid[i][j] = new Knight(i,j, player);
		} else if (p.equals("Q") || p.equals("q")) {
			grid[i][j] = new Queen(i,j, player);
		}else if (p.equals("K") || p.equals("k")) {
			grid[i][j] = new King(i,j, player);
		}else if (p.equals("R") || p.equals("r")) {
			grid[i][j] = new Rook(i,j, player);
		}else if (p.equals("P") || p.equals("p")) {
			grid[i][j] = new Pawn(i,j, player);
		}else if (p.equals("B") || p.equals("b")) {
			grid[i][j] = new Bishop(i,j, player);
		}else{
			grid[i][j] = null;
		}
	}
	
	/**
	 * Takes a string input from the user, takes the first character and converts
	 * it into an integer corresponding to the x-coordinate on the chess board.
	 * @param position: The position on the chess board (i.e. A6).
	 * @return The x coordinate of the position as an integer.
	 * @throws IOException
	 * @author - Vivian Andreeva
	 */
	public static int convertX(String position) throws IOException {
		// Ex: Position input by user is A6. convertX converts the letter "A" into 
		// a numerical value (0-7) to use in the chess grid.
		StringReader in = new StringReader(position);
		// Reads first character, in this case, "A".
		int c = in.read();
		// Converts character into a usable String, s. 
		String s = Character.toString((char)c);
		// Compare string to various APPLICABLE letters. 
		// If the input is valid, it returns the integer corresponding to the specific letter
		// as the x coordinate (A = 0).
		if (s.compareTo("A") == 0){
			return 0;
		}
		else if (s.compareTo("B") == 0) {
			return 1;
		}
		else if (s.compareTo("C") == 0) {
			return 2;
		}
		else if (s.compareTo("D") == 0) {
			return 3;
		}
		else if (s.compareTo("E") == 0) {
			return 4;
		}
		else if (s.compareTo("F") == 0) {
			return 5;
		}
		else if (s.compareTo("G") == 0) {
			return 6;
		}
		else if (s.compareTo("H") == 0) {
			return 7;
		}
		else if (s.compareTo("R") ==0) {
			return 8;
		}
		//If the letter is not applicable to any of the letters (A-H), the input 
		// is invalid (returns -1) and must be retyped.
		return -1;
	}
	/**
	 * Takes a string input from the user, takes the second character and returns
	 * the integer corresponding to the y-coordinate on the chess board.
	 * @param position: The position on the chess board (i.e. A6).
	 * @return The y coordinate of the position as an integer. 
	 * @throws IOException
	 * @author - Vivian Andreeva
	 */
	public static int convertY(String position) throws IOException {
		// Ex: Position input by user is A6. convertY takes the second character, (6 in this
		// example) and returns it as an integer y coordinate.
		StringReader in = new StringReader(position);
		int i = 0;
		int c = 0;
		do {
			c = in.read();
			i++;
		// Loop through this function twice so that c is equal to the second character, 6, and
		// not the first, A. 
		} while (i<2);
		int a = Integer.parseInt(Character.toString((char) c));
		return (a-1);
	}
	
	/**
	 * Converts the grid and the pieces that are currently placed on it into a 
	 * printable string 
	 * @return - "|_|" if the cell is empty or "|Letter|" corresponding to the
	 * chess piece currently placed at those coordinates (i.e Queen - "Q")
	 * @modified Vivian Andreeva
	 */
	public String toString(){
		String cell = "  A  B  C  D  E  F  G  H \n";
        for(int j = 0; j < SIZE; j++){
        	cell += j+1;
            for(int i = 0; i < SIZE; i++){
            			if (grid[i][j] == null) {
            				cell += "|_|";
            			} else {
            				cell += ("|" + grid[i][j].toString() + "|"); // if cell is empty print _
            			}
            }
            cell += "\n";
         } 
        return cell;
    }  
	
	/**
	 * Uses the returned string of the board from method toString and prints
	 * the board.
	 */
	public void print(){
		System.out.println(this);
	}
}
